package cn.yuxiaoyuan.controller.manager;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.annotation.SaCheckRole;
import cn.dev33.satoken.annotation.SaIgnore;
import cn.dev33.satoken.stp.SaLoginConfig;
import cn.dev33.satoken.stp.SaTokenInfo;
import cn.dev33.satoken.stp.StpUtil;
import cn.yuxiaoyuan.pojo.ro.LoginRo;
import cn.yuxiaoyuan.pojo.vo.LoginInfoVo;
import cn.yuxiaoyuan.service.ILoginService;
import cn.yuxiaoyuan.pojo.domain.Login;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import javax.validation.Valid;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import cn.yuxiaoyuan.pojo.query.PageQueryWrapper;
import cn.yuxiaoyuan.result.JSONResult;
import cn.yuxiaoyuan.result.PageResult;

@Tag(name = "登录表",description = "登录表")
@RestController
@RequestMapping("/manager/login")
public class LoginController{

    @Autowired
    public ILoginService loginService;


    @GetMapping("/login/{code}/{phone}")
    @SaIgnore
    public JSONResult login(@PathVariable("code") String code,@PathVariable("phone")String  phone){
        //拿到code去访问微信的服务器换取openid和session_key
//        OpenIdResult openIdResult = wechatLoginTemplate.getWechatAccessTokenUrl(code);
//        String openid = openIdResult.getOpenid();
//
//        Driver one = driverService.getOne(new LambdaQueryWrapper<Driver>().eq(Driver::getOpenId, openid));
        Login one = loginService.getOne(new LambdaQueryWrapper<Login>().eq(Login::getPhone, phone));
        //有 登录  没有请注册
        if(one == null){
            return JSONResult.error("请先注册");
        }
        //登录
        loginService.initUserRoleAndPermission(one);


        //构建返回对象
        LoginInfoVo loginInfoVo = new LoginInfoVo();
        loginInfoVo.setAvatar(one.getAvatar());
        loginInfoVo.setPhone(one.getPhone());
        loginInfoVo.setNickName(one.getNickName());
        loginInfoVo.setToken(StpUtil.getTokenValue());
        loginInfoVo.setId(StpUtil.getLoginIdAsLong());
        return JSONResult.success(loginInfoVo);
    }

    @PostMapping("/login")
    @SaIgnore
    public JSONResult login(@RequestBody @Valid Login login){
       return loginService.adminLogin(login);
    }



    @Operation( summary= "登录",description = "登录接口")
    @Parameter(name = "login",description = "登录的对象",required = true)
    @PostMapping("/admin")
    public JSONResult login(@RequestBody @Valid LoginRo loginRo){
        JSONResult jsonResult = loginService.login(loginRo);
        return jsonResult;
    }

    @Operation( summary= "保存Login",description = "基础对象保存接口")
    @Parameter(name = "login",description = "保存的对象",required = true)
    @PostMapping
    public JSONResult save(@RequestBody @Valid Login login){
        return JSONResult.success(loginService.save(login));
    }

    @Operation( summary= "修改Login",description = "基础对象修改接口")
    @Parameter(name = "login",description = "修改的对象",required = true)
    @PutMapping
    public JSONResult update(@RequestBody  @Valid Login login){
        return JSONResult.success(loginService.updateById(login));
    }

    //删除对象
    @Operation( summary= "删除Login",description = "基础对象删除接口，采用状态删除")
    @Parameter(name = "id",description = "删除的对象ID",required = true)
    @DeleteMapping(value="/{id}")
    public JSONResult delete(@PathVariable("id") Long id){
        return JSONResult.success(loginService.removeById(id));
    }

    //获取对象
    @Operation( summary= "获取Login",description = "基础对象获取接口")
    @Parameter(name = "id",description = "查询的对象ID",required = true)
    @GetMapping(value = "/{id}")
    @SaCheckPermission("login:get")
    @SaCheckRole("admin")
    public JSONResult get(@PathVariable("id")Long id){
        return JSONResult.success(loginService.getById(id));
    }

    //获取列表:PageQueryWrapper作为通用的查询对象
    @Operation( summary= "查询Login列表",description = "基础对象列表查询，不带分页")
    @Parameter(name = "query",description = "查询条件",required = true)
    @PostMapping(value = "/list")
    public JSONResult list(@RequestBody PageQueryWrapper<Login> query){
        QueryWrapper<Login> wrapper = new QueryWrapper<>();
        //实体类作为查询条件
        wrapper.setEntity(query.getQuery());
        return JSONResult.success(loginService.list(wrapper));
    }

    //分页查询
    @Operation( summary= "查询Login分页列表",description = "基础对象列表查询，带分页")
    @Parameter(name = "query",description = "查询条件，需要指定分页条件",required = true)
    @PostMapping(value = "/pagelist")
    public JSONResult page(@RequestBody PageQueryWrapper<Login> query){
        //分页查询
        Page<Login> page = new Page<Login>(query.getPage(),query.getRows());
        QueryWrapper<Login> wrapper = new QueryWrapper<>();
        //实体类作为查询条件
        wrapper.setEntity(query.getQuery());
        //分页查询
        page = loginService.page(page,wrapper);
        //返回结果
        return JSONResult.success(new PageResult<Login>(page.getTotal(),page.getRecords()));
    }

}
